package <packageRoot>;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.com.fmz.dao.GenericDAO;
import <packageEntity>;

@ManagedBean
@RequestScoped
public class <EntityName>MB {
	
	private <EntityName> <EntityVariable> = new <EntityName>();
	
	private GenericDAO<<EntityName>> genericDAO = new GenericDAO<<EntityName>>();
	
	private List<<EntityName>> list<EntityName> = new ArrayList<<EntityName>>();
	
	@PostConstruct
	public void init() {
		try {
			<EntityVariable> = new <EntityName>();
			list<EntityName> = genericDAO.list(genericDAO.createQuery("select a from <EntityName> a"));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void save<EntityName>() {
		try {
			if (<EntityVariable>.getId() != 0) {
				genericDAO.merge(<EntityVariable>);
			}
			else {
				genericDAO.persist(<EntityVariable>);
			}
			init();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}	

	public void delete<EntityName>(){
		try {
			genericDAO.remove(<EntityName>.class, <EntityVariable>.getId());
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setList<EntityName>(List<<EntityName>> list<EntityName>) {
		this.list<EntityName> = list<EntityName>;
	}

	public List<<EntityName>> getList<EntityName>() {
		return list<EntityName>;
	}
	
	public <EntityName> get<EntityName>() {
		return <EntityVariable>;
	}
	
	public void set<EntityName>(<EntityName> <EntityVariable>) {
		this.<EntityVariable> = <EntityVariable>;
	}
}
